package org.gdstash.ui.util;

import com.jidesoft.combobox.QuickComboBoxFilterField;

import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.plaf.basic.BasicComboPopup;
import java.awt.*;

/**
 * TODO: this
 *
 * @author pengzhikang
 * @version 1.0
 * @date 2025/1/19 21:49
 */
public class ComboBoxSearchPanel<T> extends JPanel {

    private JComboBox<T> comboBox;
    private DefaultComboBoxModel<T> comboBoxModel;
    private QuickComboBoxFilterField boxFilterField;

    public ComboBoxSearchPanel() {
        final BorderLayout layout = new BorderLayout();
        this.setLayout(layout);

        this.comboBox = new WideComboBox<>();

        this.boxFilterField = new QuickComboBoxFilterField();

        add(BorderLayout.NORTH, boxFilterField);
        add(BorderLayout.CENTER, comboBox);
    }

    /**
     * 设定模型
     *
     * @param comboBoxModel 组合框模型
     * @author pengzhikang
     * @date 2025/01/19
     */
    public void setModel(DefaultComboBoxModel<T> comboBoxModel) {
        this.comboBoxModel = comboBoxModel;
        this.boxFilterField.setComboBoxModel(comboBoxModel);

        this.boxFilterField.setComboBox(comboBox);

        this.comboBox.setModel(boxFilterField.getDisplayComboBoxModel());
    }

    public JComboBox<T> getComboBox() {
        return comboBox;
    }

    public QuickComboBoxFilterField getBoxFilterField() {
        return boxFilterField;
    }

    public void removeAllItems() {
        if (this.comboBoxModel != null) {
            this.comboBoxModel.removeAllElements();
        }
    }
}


